const superagent = require('superagent');
require('superagent-charset')(superagent)
//nodejs里一个非常方便的客户端请求代理模块
const cheerio = require('cheerio');
//Node.js 版的jQuery
// const async = require('async');

const fs = require('fs');
//fs操作IO
const url = require('url');
//url模块
// var querystring = require('querystring')
var path = require('path')

// 启动log
const log4js = require('log4js');
log4js.configure({
    appenders: { cheese: { type: 'file', filename: './getSDdata.log' } },
    categories: { default: { appenders: ['cheese'], level: 'trace' } }
});
const logger = log4js.getLogger('cheese');
// logger.debug('hahaha')

// superagent = charset(superagent);
// 爬漫猫SD敢达数据
const getter = require('./getter')
var maxNum = 0
var i = 1

startGet(i)

function startGet(i) {
    console.log(`开始获取第${i}页`)
    superagent.get(`http://home.xhguo.net:41004/search-unit?o=&r=&wt=&rtg=&order=&w=&wex=&g=&sk=&q=&cps=&rt=&page=${i}`)
        .then(res => {
            logger.info(`page${i} 获取OK！`)
            if (!maxNum) {
                let $ = cheerio.load(res.text)
                maxNum = $($($(".search-unit-list")[0]).prev()).text().match(/\d+/)[0]
            }
            getter.getPageData(res.text, { maxNum: maxNum }, function (result, data) {
                if (data.length >= maxNum) {
                    // 存储机体数据
                    fs.writeFile('./SDdata.json', JSON.stringify(data), function (err) {
                        if (err) {
                            console.log(err)
                        }
                        logger.info(`存储敢达数据完毕！累死本宝宝了！共存储了${data.length}`)
                        console.log(`存储敢达数据完毕！累死本宝宝了！共存储了${data.length} 数据`)
                    })
                    // 存储武器、技能图片
                    // getter.getOtherPics()
                }
                if (data.length >= maxNum) {
                    // 存储机体数据
                    fs.writeFile('./SDdatatmp.json', JSON.stringify(data), function (err) {
                        if (err) {
                            console.log(err)
                        }
                    })
                }

                if (i < 77) {
                    i++
                    startGet(i)
                }
            })
            // fs.writeFileSync('./cheerio-test.js',res.text)
            // let num = $($($('.search-unit-list').find('tr')[2]).find('th')[0]).text()
            // console.log(num)
        })
        .catch(err => {
            // err.message, err.response
            logger.error('获取page' + i + '失败')
            console.log('获取page' + i + '失败,重新获取当前页。')
            setTimeout(() => {
                startGet(i)
            }, 5000);
        })
}

